﻿using Lis.IDAL;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lis.KISSFRSTDAL
{
    public class BookInfoDal : BaseDal<BookInfo>, IBookInfoDal
    {
        #region 01，根据书名查询图书信息+IEnumerable<BookInfo> GetEntitiesByName(string name)
        /// <summary>
        /// 根据书名查询图书信息
        /// </summary>
        /// <param name="name">书名</param>
        /// <returns></returns>
        public IEnumerable<BookInfo> GetEntitiesByName(string name)
        {
            return DB.QueryEntities<BookInfo>("Lis.Book.Select.OneByType", new { ZTM = name });
        }
        #endregion

        #region 02，根据作者查询图书信息+IEnumerable<BookInfo> GetEntitiesByAuthor(string author)
        /// <summary>
        /// 根据作者查询图书信息
        /// </summary>
        /// <param name="author">作者</param>
        /// <returns></returns>
        public IEnumerable<BookInfo> GetEntitiesByAuthor(string author)
        {
            return DB.QueryEntities<BookInfo>("Lis.Book.Select.OneByType", new { ZRZ = author });
        }
        #endregion

        #region 03，根据isbn号进行图书查询+IEnumerable<BookInfo> GetEntityByISBN(string isbn)
        /// <summary>
        /// 根据isbn号进行图书查询
        /// </summary>
        /// <param name="isbn">isbn</param>
        /// <returns></returns>
        public IEnumerable<BookInfo> GetEntityByISBN(string isbn)
        {
            return DB.QueryEntities<BookInfo>("Lis.Book.Select.OneByISBN", new { ISBN = isbn });
        } 
        #endregion

        #region 04，根据出版社查询图书信息+IEnumerable<BookInfo> GetEntitiesByPublish(string publish)
        /// <summary>
        /// 根据出版社查询图书信息
        /// </summary>
        /// <param name="publish">出版社</param>
        /// <returns></returns>
        public IEnumerable<BookInfo> GetEntitiesByPublish(string publish)
        {
            return DB.QueryEntities<BookInfo>("Lis.Book.Select.OneByType", new { CBS = publish });
        } 
        #endregion

        #region 05，分页查询+override IQueryable<BookInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<BookInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<BookInfo, S>> orderByLambda)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="total">总条数</param>
        /// <param name="isAsc">是否排序</param>
        /// <param name="whereLambda">查询lambda</param>
        /// <param name="orderByLambda">排序lambda</param>
        /// <returns></returns>
        public override IQueryable<BookInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<BookInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<BookInfo, S>> orderByLambda)
        {
            var list = DB.QueryEntities<BookInfo>("Lis.Book.Select.OneByType").ToList();
            total = list.AsQueryable<BookInfo>().Where(whereLambda).Count();
            if (isAsc)
            {
                return list.AsQueryable<BookInfo>().Where(whereLambda)
                    .OrderBy<BookInfo, S>(orderByLambda)
                    .Skip(pageSize * (pageIndex - 1))
                    .Take(pageSize);
            }
            else
            {
                return list.AsQueryable<BookInfo>().Where(whereLambda)
                      .OrderBy<BookInfo, S>(orderByLambda)
                      .Skip(pageSize * (pageIndex - 1))
                      .Take(pageSize);
            }
        } 
        #endregion
    }
}
